package com.hanxu.mallproduct.controller;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.hanxu.mallproduct.entity.AttrEntity;
import com.hanxu.mallproduct.service.AttrAttrgroupRelationService;
import com.hanxu.mallproduct.service.AttrService;
import com.hanxu.mallproduct.service.CategoryService;
import com.hanxu.mallproduct.vo.AttrGroupRelationVo;
import com.hanxu.mallproduct.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hanxu.mallproduct.entity.AttrGroupEntity;
import com.hanxu.mallproduct.service.AttrGroupService;
import com.hanxu.common.utils.PageUtils;
import com.hanxu.common.utils.R;


/**
 * 属性分组
 *
 * @author hanxu
 * @email hanxu@gmail.com
 * @date 2025-03-16 20:27:38
 */
@RestController
@RequestMapping("mallproduct/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private AttrService attrService;

    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable Long attrgroupId) {
        // 根据分组id查找关联的所有基本属性
        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", entities);
    }


    /**
     * 列表
     */
    @RequestMapping("/list/{categoryId}")
    //@RequiresPermissions("mallproduct:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable Long categoryId) throws UnsupportedEncodingException {
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, categoryId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("mallproduct:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        System.out.println("打印：" + attrGroupId);
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("mallproduct:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("mallproduct:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("mallproduct:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 删除属性与分组的关联关系
     */
    @PostMapping("/attr/relation/delete")
    //@RequiresPermissions("mallproduct:attrgroup:delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {
        attrService.deleteRelation(vos);
        return R.ok();
    }

    /**
     * 获取当前分组没有关联的所有属性
     *
     * @param attrgroupId
     * @param params
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    //@RequiresPermissions("mallproduct:attrgroup:delete")
    public R attrNoRelation(@PathVariable Long attrgroupId, @RequestParam Map<String, Object> params) throws UnsupportedEncodingException {
        PageUtils page = attrService.getNoRelationAttr(params, attrgroupId);
        return R.ok().put("page", page);
    }

    /**
     * 新增关联关系
     *
     * @param vos
     * @return
     */
    @PostMapping("/attr/relation")
    public R attrRelation(@RequestBody List<AttrGroupRelationVo> vos) {
        attrAttrgroupRelationService.saveBatch(vos);
        return R.ok();
    }

    /**
     * 获取分类下所有分组和关联属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R withAttr(@PathVariable Long catelogId) {
        // 查出当前分类下的所有属性分组，查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);

        return R.ok().put("data", vos);
    }
}
